import numpy as np
import matplotlib.pyplot as plt
import time
import requests
import pandas as pd



def play():
    # 日期
    today = time.strftime('%Y{y}%m{m}%d{d}',time.localtime()).format(y='年',m='月',d='日')
    # 网址
    url = 'https://arena.360.cn/api/service/data/ncov-live-3'
    # 请求头
    headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36'
            }
    # 发送请求
    response = requests.get(url=url,headers=headers)

    # 得到相应的json数据
    json = response.json()
    # 数据列表
    city = []
    diagnosed = []
    suspected = []
    cured = []
    print(json)
    if json.get('data'):
        for item in json.get('data')['detail']:
            city.append(item['city'])
            diagnosed.append(item['diagnosed']) 
            suspected.append(item['suspected'])
            cured.append(item['cured'])

    # 利用pandas保存文件
    df = pd.DataFrame()
    df['城市'] = city
    df['确诊'] = diagnosed
    df['疑似'] = suspected
    df['治愈'] = cured
    df.to_csv(today+'全国疫情数据.csv',mode='w',index=None,encoding='gbk')
    print('保存完成!')

if __name__ == '__main__':
    play()